package com.sztx.pay.center.core.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.core.service.BillnoService;
import com.sztx.pay.center.core.service.OrderMappingService;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderMappingMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.OrderMappingDO;
import com.sztx.se.common.util.validate.ValidateUtil;
@Service
public class OrderMappingServiceImpl implements OrderMappingService {
	
	@Autowired
	OrderMappingMysqlDAO orderMappingMysqlDAO;
	
	@Autowired
	BillnoService billnoService;
	
	@Override
	public String addMappingOrder(String tradeOrderId, Byte tradeOrderType) {
		ValidateUtil.isNotBlank(tradeOrderId, "交易订单编号不能为空");
		ValidateUtil.notNull(tradeOrderType, "交易类型不能为空");
		String mappingOrderId = billnoService.nextMappingOrderNo();
		
		OrderMappingDO orderMappingDO = new OrderMappingDO();
		orderMappingDO.setTradeOrderId(tradeOrderId);
		orderMappingDO.setTradeType(tradeOrderType);
		orderMappingDO.setMappingOrderId(mappingOrderId);
		orderMappingDO.setAddTime(new Date());
		orderMappingMysqlDAO.save(orderMappingDO);
		
		return mappingOrderId;
	}

	@Override
	public String getMappingOrderId(String tradeOrderId, Byte tradeOrderType) {
		ValidateUtil.isNotBlank(tradeOrderId, "交易订单编号不能为空");
		ValidateUtil.notNull(tradeOrderType, "交易类型不能为空");
		
		OrderMappingDO orderMappingDO = new OrderMappingDO();
		orderMappingDO.setTradeOrderId(tradeOrderId);
		orderMappingDO.setTradeType(tradeOrderType);
		OrderMappingDO response = orderMappingMysqlDAO.find(orderMappingDO);
		
		if (response == null) {
			return null;
		}
		
		return response.getMappingOrderId();
	}

	@Override
	public String getTradeOrderId(String mappingOrderId) {
		ValidateUtil.isNotBlank(mappingOrderId, "映射订单编号不能为空");
		
		OrderMappingDO orderMappingDO = new OrderMappingDO();
		orderMappingDO.setMappingOrderId(mappingOrderId);
		
		OrderMappingDO response = orderMappingMysqlDAO.find(orderMappingDO);
		
		if (response == null) {
			return null;
		}
		return response.getTradeOrderId();
	}

}
